//---------------------------------------------------------------------------

#include <vcl.h>
#include <jpeg.hpp>
#pragma hdrstop

#include "uDivers.h"
#include "GenEtatVer.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)

#define MODULE_TAG MODULE_TAG_DIVERS

__fastcall EDocException::EDocException( int nNumException) : Exception( EmptyStr)
{
    m_nNumException = nNumException;
    switch( nNumException)
    {
        case Exc_Fichier_Abime       : Message = "Le fichier est soit abîmé soit n'est pas un fichier comportant des états EIC."; break;
        case Exc_Ecriture_impossible : Message = "Le fichier ne peut pas être enregistré. Vérifiez la place disponible sur le disque, ainsi que son accès en écriture."; break;
        case Exc_Version_posterieure : Message = "Le fichier est dans une version postérieure à celle du visualisateur d'état."
                                                 " Il ne peut pas être lu avec cette version."; break;
        default : Message = "????";
    }
};
//---------------------------------------------------------------------------
TFlux* __fastcall NewTFlux(void)           { return new TFlux();}
void   __fastcall DeleteTFlux(TFlux* Flux) { delete Flux;}
//---------------------------------------------------------------------------
__fastcall TFlux::TFlux( const AnsiString &FileName, Word Mode) : TObject( )
{
    m_s = new TFileStream( FileName, Mode);
    if( Mode & fmOpenWrite)
        Ecrit( CUR_VER_FILE);
    else{
        Lit( m_FileVersion);
        if( (m_FileVersion > FILE_VERSION( 1, 0)) && ( m_FileVersion <= FILE_VERSION( 0, 1)))
             m_FileVersion = FILE_VERSION( 0, LOWORD(m_FileVersion));
        if( m_FileVersion > CUR_VER_FILE)
        {
            delete m_s; m_s = NULL;
            throw EDocException( Exc_Version_posterieure);
        }
    }
}
__fastcall TFlux::TFlux( ) : TObject( )
{
    m_s = new TMemoryStream();
    m_FileVersion = CUR_VER_FILE;
}
__fastcall TFlux::~TFlux( )
{
    if( m_s) delete m_s;
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( short &v)
{
    if( Read( (void*)&v, sizeof( v)) != sizeof( v))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( short v)
{
    if( Write( (void*)&v, sizeof( v)) != sizeof( v))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( char &c)
{
    if( Read( (void*)&c, sizeof( c)) != sizeof( c))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( char c)
{
    if( Write( (void*)&c, sizeof( c)) != sizeof( c))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( byte &b)
{
    if( Read( (void*)&b, sizeof( b)) != sizeof( b))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( byte b)
{
    if( Write( (void*)&b, sizeof( b)) != sizeof( b))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( bool &b)
{
    if( Read( (void*)&b, sizeof( b)) != sizeof( b))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( bool b)
{
    if( Write( (void*)&b, sizeof( b)) != sizeof( b))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( WORD &w)
{
    if( Read( (void*)&w, sizeof( w)) != sizeof( w))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( WORD w)
{
    if( Write( (void*)&w, sizeof( w)) != sizeof( w))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( int &i)
{
    if( Read( (void*)&i, sizeof( i)) != sizeof( i))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( int i)
{
    if( Write( (void*)&i, sizeof( i)) != sizeof( i))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( float &f)
{
    if( Read( (void*)&f, sizeof( f)) != sizeof( f))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( float f)
{
    if( Write( (void*)&f, sizeof( f)) != sizeof( f))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( double &d)
{
    if( Read( (void*)&d, sizeof( d)) != sizeof( d))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( double d)
{
    if( Write( (void*)&d, sizeof( d)) != sizeof( d))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( DWORD &d)
{
    if( Read( (void*)&d, sizeof( d)) != sizeof( d))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( DWORD d)
{
    if( Write( (void*)&d, sizeof( d)) != sizeof( d))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( AnsiString &s)
{
    int   nLen;
    Lit( nLen);
    char *sz   = (char*)::_LocalAlloc( TAGL, LMEM_FIXED | LMEM_ZEROINIT, nLen+1);
    bool bExcept = !sz || (Read( (void*)sz, nLen) != nLen);
    s = sz;
    ::_LocalFree( TAGL, sz);
    if( bExcept)  throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( const AnsiString &s)
{
    Ecrit( s.Length());
    if( Write( (void*)s.c_str(), s.Length()) != s.Length())
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( TColor &color)
{
    if( Read( (void*)&color, sizeof( color)) != sizeof( color))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( const TColor color, const TColor colDef)
{
    if( (colDef != -1) && (color == colDef) )
        Ecrit( (TColor)-1, (TColor)0);
    else if( Write( (void*)&color, sizeof( color)) != sizeof( color))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( TPenStyle &PenStyle)
{
    if( Read( (void*)&PenStyle, sizeof( PenStyle)) != sizeof( PenStyle))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( const TPenStyle PenStyle)
{
    if( Write( (void*)&PenStyle, sizeof( PenStyle)) != sizeof( PenStyle))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( TBrushStyle &BrushStyle)
{
    if( Read( (void*)&BrushStyle, sizeof( BrushStyle)) != sizeof( BrushStyle))
        throw EDocException( Exc_Fichier_Abime);
}
void __fastcall TFlux::Ecrit( const TBrushStyle BrushStyle)
{
    if( Write( (void*)&BrushStyle, sizeof( BrushStyle)) != sizeof( BrushStyle))
        throw EDocException( Exc_Ecriture_impossible);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( EICFont **ppFont)
{
    bool             UseDefault;

    Lit( UseDefault);
    if( !UseDefault)
    {
        *ppFont = new EICFont( this);
    }
    else *ppFont = NULL;
}
void __fastcall TFlux::Ecrit( EICFont *pFont, EICFont *pFontDefaut)
{
    if( pFont && (!pFontDefaut || (*pFont != *pFontDefaut)))
    {
        Ecrit( (bool)false);
        pFont->Enregistre( this);
    }
    else
        Ecrit( (bool)true);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( EICCurrencyFmt **ppCurrencyFormat)
{
    bool             UseDefault;

    Lit( UseDefault);
    if( !UseDefault)
    {
        AnsiString CurrencySymbol;
        if( FileVersion <= FILE_VERSION( 1, 12))
            Lit( CurrencySymbol);

        *ppCurrencyFormat = new EICCurrencyFmt(this, CurrencySymbol);
    }
    else
       *ppCurrencyFormat = NULL;

}
void __fastcall TFlux::Ecrit( const EICCurrencyFmt *pCurrencyFormat, const EICCurrencyFmt *pCurrencyFormatDefault)
{
    if( pCurrencyFormat &&
        ( !pCurrencyFormatDefault || (*pCurrencyFormat != *pCurrencyFormatDefault) ) )
    {
        Ecrit( (bool)false);
        pCurrencyFormat->Enregistre( this);
    }
    else
        Ecrit( (bool)true);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( EICNumberFmt **ppNumberFormat)
{
    bool             UseDefault;

    Lit( UseDefault);
    if( !UseDefault)
        *ppNumberFormat = new EICNumberFmt(this);
    else
       *ppNumberFormat = NULL;

}
void __fastcall TFlux::Ecrit( const EICNumberFmt *pNumberFormat, const EICNumberFmt *pNumberFormatDefault)
{
    if( pNumberFormat &&
        ( !pNumberFormatDefault || (*pNumberFormat != *pNumberFormatDefault) ) )
    {
        Ecrit( (bool)false);
        pNumberFormat->Enregistre( this);
    }
    else
        Ecrit( (bool)true);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( TStringList *Lst)
{
    int n;
    AnsiString s;
    Lit( n);
    for( int i=0; i < n; i++)
    {
        Lit( s);
        Lst->Add( s);
    }
}
void __fastcall TFlux::Ecrit( TStringList *Lst)
{
    Ecrit( Lst->Count);
    for( int i=0; i < Lst->Count; i++)
        Ecrit( Lst->Strings[ i]);
}
//---------------------------------------------------------------------------
void __fastcall TFlux::Lit( TParams** pParams)
{
    int n, FldType;
    AnsiString ParamName, Value;
    Lit( n);
    if( n) (*pParams) = new TParams();
    for( int i=0; i < n; i++)
    {
        Lit( FldType);
        Lit( ParamName);
        Lit( Value);
        (*pParams)->CreateParam( (TFieldType)FldType, ParamName, ptInput)->Text = Value;
    }
}

void __fastcall TFlux::Ecrit( TParams* Params)
{
    if( !Params){ Ecrit ( 0); return;}
    Ecrit( Params->Count);
    for( int i=0; i < Params->Count; i++)
    {
        Ecrit( Params->Items[ i]->DataType);
        Ecrit( Params->Items[ i]->Name);
        Ecrit( Params->Items[ i]->Text);
    }
}
//---------------------------------------------------------------------------
void ChargeImage( TStream *Stream, TGraphic *&g, TGraphic *gFmt)
{
    try{
        g = gFmt;
        Stream->Position = 0;
        gFmt->LoadFromStream( Stream);
    }
    catch( ...) { delete gFmt; g = NULL; }
}

void ChargeImage( TPicture **pPicture, TStream *Stream)
{
    TGraphic        *g = NULL;
            ChargeImage( Stream, g, new Graphics::TBitmap());
    if( !g) ChargeImage( Stream, g, new TJPEGImage());
    if( !g) ChargeImage( Stream, g, new Graphics::TMetafile());
    if( !g) ChargeImage( Stream, g, new Graphics::TIcon());

    if( g)
    {
        (*pPicture) = new TPicture();
        (*pPicture)->Assign( g);
        delete g; g = NULL;
    }

}
void __fastcall TFlux::Lit( TPicture** pPicture)
{
    int nTaille;
    TMemoryStream	*Stream = NULL;
    try{
        Lit( nTaille);

        Stream = new TMemoryStream();
        Stream->SetSize( nTaille);
        if( Read( Stream->Memory, nTaille) != nTaille)
            throw EDocException( Exc_Fichier_Abime);

        ChargeImage( pPicture, Stream);

    }__finally
    {
        delete Stream;
    }
}

void __fastcall TFlux::Ecrit( TPicture* Picture)
{
    TMemoryStream	*Stream = NULL;
    try{
        Stream = new TMemoryStream();
        Picture->Graphic->SaveToStream( Stream);
        Ecrit( Stream->Size);
        if( Write( Stream->Memory, Stream->Size) != Stream->Size)
            throw EDocException( Exc_Ecriture_impossible);
    }__finally
    {
        delete Stream;
    }
}

//---------------------------------------------------------------------------

void __fastcall TCoordonnee::Lit( TFlux *f)
{
    if( f->FileVersion <= FILE_VERSION( 0, 6)){
        int i;
        f->Lit( i);
        m_Twips = i;
    }else
        f->Lit( m_Twips);
}
void __fastcall TCoordonnee::Enregistre( TFlux *f)const
{
    f->Ecrit( m_Twips);
}
//---------------------------------------------------------------------------
void __fastcall TCoorPoint::Lit( TFlux *f)
{
    if( f->FileVersion <= FILE_VERSION( 1, 12)){
        TPoint p;
        f->Lit( (int)p.x);
        f->Lit( (int)p.y);
        PutPointTwips(p);
    }else{
        m_X.Lit( f);
        m_Y.Lit( f);
    }
}
void __fastcall TCoorPoint::Enregistre( TFlux *f)const
{
    m_X.Enregistre( f);
    m_Y.Enregistre( f);
}
//---------------------------------------------------------------------------
void __fastcall TCoorRect::Lit( TFlux *f)
{
    if( f->FileVersion <= FILE_VERSION( 1, 12)){
        TRect r;
        f->Lit( (int)r.left);
        f->Lit( (int)r.top);
        f->Lit( (int)r.right);
        f->Lit( (int)r.bottom);
        PutRectTwips(r);
    }else{
        m_Left.Lit( f);
        m_Top.Lit( f);
        m_Right.Lit( f);
        m_Bottom.Lit( f);
    }
}
void __fastcall TCoorRect::Enregistre( TFlux *f)const
{
    m_Left.Enregistre( f);
    m_Top.Enregistre( f);
    m_Right.Enregistre( f);
    m_Bottom.Enregistre( f);
}
//---------------------------------------------------------------------------
EICFont* EICFont::CreateEICFont( TFont* Font)
{
  EICFont *f = new EICFont();
  f->Assign(Font);
  return f;
}

__fastcall EICFont::EICFont(void):TFont()
{
  Name = "Arial";
  PixelsPerInch = Glob->ResolEcran;
}

__fastcall EICFont::EICFont( TFlux *f):TFont()
{
    Byte             lCharset;
    TColor           lColor;
    int              lHeight;
    AnsiString       lName;
    int              lPitch;
    int              lSize;
    char             cStyle = 0x00;
    TFontStyles ftStyle;
    PixelsPerInch = Glob->ResolEcran;//Glob->Resolution;
    f->Lit( lCharset);  Charset = lCharset;
    f->Lit( lColor);    Color   = lColor;
    f->Lit( lName);     if( lName == "MS Sans Serif") Name = "Arial"; else Name = lName;
    //(*ppFont)->Name    = Name;
    f->Lit( lPitch);   Pitch   = (enum TFontPitch)lPitch;
    f->Lit( lSize);    Size    = lSize;
    f->Lit( lHeight);  Height  = lHeight;
    f->Lit( cStyle);   ftStyle.Clear();
                       if( cStyle & Font_fsBold)      ftStyle << fsBold;
                       if( cStyle & Font_fsItalic)    ftStyle << fsItalic;
                       if( cStyle & Font_fsUnderline) ftStyle << fsUnderline;
                       if( cStyle & Font_fsStrikeOut) ftStyle << fsStrikeOut;
    Style = ftStyle;
}
void __fastcall EICFont::Enregistre( TFlux *f)const
{
    char cStyle = 0x00;
    if( Style.Contains( fsBold))      cStyle |= Font_fsBold;
    if( Style.Contains( fsItalic))    cStyle |= Font_fsItalic;
    if( Style.Contains( fsUnderline)) cStyle |= Font_fsUnderline;
    if( Style.Contains( fsStrikeOut)) cStyle |= Font_fsStrikeOut;

    f->Ecrit( Charset);
    f->Ecrit( Color, (TColor)-1);
    f->Ecrit( Name);
    f->Ecrit( (int)Pitch);
    f->Ecrit( Size);
    f->Ecrit( Height);
    f->Ecrit( cStyle);
}
//---------------------------------------------------------------------------




static int CorrespNegCurrNegNumber[]= {
0,//"($1.1)"
1,//"-$1.1"
1,//"$-1.1"
3,//"$1.1-"
0,//"(1.1$)"
1,//"-1.1$"
3,//"1.1-$"
3,//"1.1$-"
1,//"-1.1 $"
2,//"-$ 1.1"
4,//"1.1 $-"
3,//"$ 1.1-"
1,//"$ -1.1"
3,//"1.1- $"
0,//"($ 1.1)"
0};//"(1.1 $)"
char EICNumberFmt::sDecimalSep[2] = {'\0','\0'};
//---------------------------------------------------------------------------
__fastcall EICNumberFmt::EICNumberFmt(const NUMBERFMT *NumFmt):TObject()
{
    sDecimalSep[0]  = DecimalSeparator;
    NumDigits       = NumFmt->NumDigits;
    LeadingZero     = NumFmt->LeadingZero;
    Grouping        = NumFmt->Grouping;
    DecimalSep      = AnsiString( NumFmt->lpDecimalSep);
    ThousandSep     = AnsiString( NumFmt->lpThousandSep);
    NegativeOrder   = NumFmt->NegativeOrder;
    ZeroSignificatif = true;
    m_NbUniteArrondies = 0;
    DecimaleZeroSignificatif = true;
}
__fastcall EICNumberFmt::EICNumberFmt():TObject()
{
     char sz[ 10];
    sDecimalSep[0]  = DecimalSeparator;
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_IDIGITS,  sz, sizeof( sz)-1);
    m_NumDigits       = atoi( sz);
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_ILZERO,  sz, sizeof( sz)-1);
    m_LeadingZero     = atoi( sz);
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_SMONGROUPING,  sz, sizeof( sz)-1);
    m_Grouping        = (int)sz[0] - (int)'0';
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_SDECIMAL,  sz, sizeof( sz)-1);
    m_DecimalSep      = sz;
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_STHOUSAND,  sz, sizeof( sz)-1);
    m_ThousandSep     = sz;
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_INEGNUMBER,  sz, sizeof( sz)-1);
    m_NegativeOrder   = atoi( sz);
    m_ZeroSignificatif = true;
    m_NbUniteArrondies = 0;
    m_DecimaleZeroSignificatif = true;    
}
__fastcall EICNumberFmt::EICNumberFmt( TFlux *f)
{
    int i; short s;
    sDecimalSep[0]  = DecimalSeparator;
    f->Lit( m_DecimalSep);
    f->Lit( m_ThousandSep);
    f->Lit( i); m_NumDigits = (UINT)i;
    f->Lit( i); m_LeadingZero = (UINT)i;
    f->Lit( i); m_Grouping = (UINT)i;
    f->Lit( i); m_NegativeOrder = (UINT)i;
    if( f->FileVersion >= FILE_VERSION( 1, 2))
        f->Lit( m_ZeroSignificatif);
    if( f->FileVersion >= FILE_VERSION( 2, 0))
        f->Lit( m_NbUniteArrondies);
    if( f->FileVersion >= FILE_VERSION( 2, 8))
    {
        f->Lit( s); m_SignePositif = (eSignePositif)s;
    }else m_SignePositif = sp_Aucun;
    if( f->FileVersion >= FILE_VERSION( 2, 15))
        f->Lit( m_DecimaleZeroSignificatif);
    else
        m_DecimaleZeroSignificatif = true;
}
//---------------------------------------------------------------------------
void __fastcall EICNumberFmt::Enregistre( TFlux *f)const
{
    f->Ecrit( m_DecimalSep);
    f->Ecrit( m_ThousandSep);
    f->Ecrit( (int)m_NumDigits);
    f->Ecrit( (int)m_LeadingZero);
    f->Ecrit( (int)m_Grouping);
    f->Ecrit( (int)m_NegativeOrder);
    f->Ecrit( m_ZeroSignificatif);
    f->Ecrit( m_NbUniteArrondies);
    f->Ecrit( (short)m_SignePositif);
    f->Ecrit( m_DecimaleZeroSignificatif);    
}


NUMBERFMT* __fastcall EICNumberFmt::GetNumberFmt()const
{
    static NUMBERFMT NumFmt;
    NumFmt.NumDigits       = m_NumDigits;
    NumFmt.LeadingZero     = m_LeadingZero;
    NumFmt.Grouping        = m_Grouping;
    NumFmt.lpDecimalSep    = m_DecimalSep.IsEmpty()? sDecimalSep : m_DecimalSep.c_str();
    NumFmt.lpThousandSep   = m_ThousandSep.c_str();
    if( dynamic_cast<const EICCurrencyFmt*const>(this) ||
        ( NegativeOrder <  (sizeof(CorrespNegCurrNegNumber[0])/sizeof(CorrespNegCurrNegNumber))))
        NumFmt.NegativeOrder = CorrespNegCurrNegNumber[ NegativeOrder];
    else
        NumFmt.NegativeOrder = NegativeOrder;
    return &NumFmt;
}
void __fastcall EICNumberFmt::SetNumDigits(UINT u)
{
    if( u <= 999) m_NumDigits = u;
}
void __fastcall EICNumberFmt::SetLeadingZero(UINT u)
{
    if( u <= 99) m_LeadingZero = u;
}
void __fastcall EICNumberFmt::SetGrouping(UINT u)
{
    if( u <= 9) m_Grouping = u;
}
void __fastcall EICNumberFmt::SetNegativeOrder(UINT u)
{
    if( u <= 4) m_NegativeOrder = u;
}

AnsiString __fastcall EICNumberFmt::Arrondi( const AnsiString &s, bool &bPositif)const
{
    AnsiString as = s;
     // on remplace
    int i = as.Pos( ",");
    if( !i) i = as.Pos( ".");
    if( i) { as.Delete( i, 1); as.Insert( AnsiString(DecimalSeparator), i); }

    if( !m_ZeroSignificatif)
    {
        if( as.ToDouble() == 0)
            return EmptyStr;
    }

    as = Round( as.ToDouble() / pow(10, m_NbUniteArrondies), m_NumDigits);
    bPositif = (as.ToDouble() > 0);

    i = as.Pos( AnsiString(DecimalSeparator));
    if( i) as[ i] = '.';

    return as;
}
AnsiString& __fastcall EICNumberFmt::FormatageFinal( AnsiString &s, bool bPositif)const
{
  int Cpt, i;
    if( bPositif)
    {
        switch( m_SignePositif)
        {
            case sp_Gauche : s = "+ " + s;
        }
    }
    i = s.Pos( AnsiString(DecimalSeparator));
    if(i)
      {
      if(!m_DecimaleZeroSignificatif)
        {
        Cpt = s.Length();
        while(s[Cpt] == '0' && Cpt > i)
          Cpt--;
        if(Cpt==i)
          Cpt--;
        s = s.SubString(1, Cpt);
        }
      }
    return s;
}


AnsiString __fastcall EICNumberFmt::Chaine( const AnsiString &s)const
{
    bool bPositif = false;
    AnsiString as = Arrondi( s, bPositif);
    if( as.IsEmpty()) return EmptyStr;

    int n = ::GetNumberFormat( LOCALE_USER_DEFAULT, 0, as.c_str(), NumberFmt, NULL, 0);
    if( n)
    {
        char *sz = new char[n+1];
        ::GetNumberFormat( LOCALE_USER_DEFAULT, 0, as.c_str(), NumberFmt, sz, n);
        as = sz;
        delete []sz;
    } //else AfficheLastErrorT( "GetNumberFormat");
    return FormatageFinal( as, bPositif);
}

bool __fastcall EICNumberFmt::operator == ( const EICNumberFmt &c)const
{
    return (c.NumDigits         == m_NumDigits) &&
           (c.LeadingZero       == m_LeadingZero) &&
           (c.Grouping          == m_Grouping) &&
           (c.DecimalSep        == m_DecimalSep) &&
           (c.ThousandSep       == m_ThousandSep) &&
           (c.NegativeOrder     == m_NegativeOrder) &&
           (c.ZeroSignificatif  == m_ZeroSignificatif) &&
           (c.NbUniteArrondies  == m_NbUniteArrondies) &&
           (c.SignePositif      == m_SignePositif) &&
           (c.DecimaleZeroSignificatif  == m_DecimaleZeroSignificatif);
}
bool __fastcall EICNumberFmt::operator != ( const EICNumberFmt &c)const
{
    return (c.NumDigits         != m_NumDigits) ||
           (c.LeadingZero       != m_LeadingZero) ||
           (c.Grouping          != m_Grouping) ||
           (c.DecimalSep        != m_DecimalSep) ||
           (c.ThousandSep       != m_ThousandSep) ||
           (c.NegativeOrder     != m_NegativeOrder) ||
           (c.ZeroSignificatif  != m_ZeroSignificatif) ||
           (c.NbUniteArrondies  != m_NbUniteArrondies) ||
           (c.SignePositif      != m_SignePositif) ||
           (c.DecimaleZeroSignificatif  != m_DecimaleZeroSignificatif);
}
void __fastcall EICNumberFmt::Assign( const EICNumberFmt &c)
{
    m_NumDigits        = c.NumDigits;
    m_LeadingZero      = c.LeadingZero;
    m_Grouping         = c.Grouping;
    m_DecimalSep       = c.DecimalSep;
    m_ThousandSep      = c.ThousandSep;
    m_NegativeOrder    = c.NegativeOrder;
    m_ZeroSignificatif = c.ZeroSignificatif;
    m_NbUniteArrondies = c.NbUniteArrondies;
    m_SignePositif     = c.SignePositif;
    m_DecimaleZeroSignificatif = c.DecimaleZeroSignificatif;    

}
void __fastcall EICNumberFmt::Assign( const NUMBERFMT &c)
{
    NumDigits      = c.NumDigits;
    LeadingZero    = c.LeadingZero;
    Grouping       = c.Grouping;
    DecimalSep     = c.lpDecimalSep;
    ThousandSep    = c.lpThousandSep;
    NegativeOrder  = c.NegativeOrder;
    ZeroSignificatif = true;
    SignePositif = sp_Aucun;
    DecimaleZeroSignificatif = true;    
}

//---------------------------------------------------------------------------

__fastcall EICCurrencyFmt::EICCurrencyFmt(const CURRENCYFMT *CurFmt):EICNumberFmt( ((const NUMBERFMT*)CurFmt))
{
    PositiveOrder   = CurFmt->PositiveOrder;
    CurrencySymbol  = AnsiString( CurFmt->lpCurrencySymbol);
}
__fastcall EICCurrencyFmt::EICCurrencyFmt():EICNumberFmt()
{
    char sz[ 10];
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS,   sz, sizeof( sz)-1);
    NumDigits       = atoi( sz);
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_INEGCURR,      sz, sizeof( sz)-1);
    NegativeOrder   = atoi( sz);
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_ICURRENCY,     sz, sizeof( sz)-1);
    PositiveOrder   = atoi( sz);
    ::GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_SCURRENCY,     sz, sizeof( sz)-1);
    CurrencySymbol  = sz;
}
__fastcall EICCurrencyFmt::EICCurrencyFmt( TFlux *f, const AnsiString &CurSymbol):EICNumberFmt(f)
{
    int i;
    if( f->FileVersion <= FILE_VERSION( 1, 12))
        m_CurrencySymbol = CurSymbol;
    else
        f->Lit( m_CurrencySymbol);
    f->Lit( i); m_PositiveOrder = (UINT)i;
}
void __fastcall EICCurrencyFmt::Enregistre( TFlux *f)const
{
    inherited::Enregistre( f);
    f->Ecrit( m_CurrencySymbol);
    f->Ecrit( (int)m_PositiveOrder);
}
CURRENCYFMT* __fastcall EICCurrencyFmt::GetCurrencyFmt()const
{
    static CURRENCYFMT CurFmt;
    memcpy( &CurFmt, NumberFmt, sizeof( *NumberFmt));
    CurFmt.NegativeOrder     = m_NegativeOrder;
    CurFmt.PositiveOrder     = m_PositiveOrder;
    CurFmt.lpCurrencySymbol  = m_CurrencySymbol.c_str();
    return &CurFmt;
}
void __fastcall EICCurrencyFmt::SetNegativeOrder(UINT u)
{
    if( u <= 15) m_NegativeOrder = u;
}
void __fastcall EICCurrencyFmt::SetPositiveOrder(UINT u)
{
    if( u <= 3) m_PositiveOrder = u;
}
AnsiString __fastcall EICCurrencyFmt::Chaine( const AnsiString &s)const
{
    bool bPositif = false;
    AnsiString as = Arrondi( s, bPositif);
    if( as.IsEmpty()) return EmptyStr;

    int n = ::GetCurrencyFormat( LOCALE_USER_DEFAULT, 0, as.c_str(), CurrencyFmt, NULL, 0);
    if( n)
    {
        char *sz = new char[n+1];
        ::GetCurrencyFormat( LOCALE_USER_DEFAULT, 0, as.c_str(), CurrencyFmt, sz, n);
        as = sz;
        as.Trim();
        delete []sz;
    }   //else AfficheLastErrorT( "GetCurrencyFormat");
    return FormatageFinal( as.TrimRight().TrimLeft(), bPositif);
}

bool __fastcall EICCurrencyFmt::operator == ( const EICCurrencyFmt &c)const
{
    return (EICNumberFmt::operator ==)( c)      &&
           (c.PositiveOrder  == m_PositiveOrder)  &&
           (c.CurrencySymbol == m_CurrencySymbol);
}
bool __fastcall EICCurrencyFmt::operator != ( const EICCurrencyFmt &c)const
{
    return (EICNumberFmt::operator !=)( c)     ||
           (c.PositiveOrder  != m_PositiveOrder) ||
           (c.CurrencySymbol != m_CurrencySymbol);
}
void __fastcall EICCurrencyFmt::Assign( const EICCurrencyFmt &c)
{
    EICNumberFmt::Assign( c);
    m_NegativeOrder   = c.NegativeOrder;
    m_PositiveOrder   = c.PositiveOrder;
    m_CurrencySymbol  = c.CurrencySymbol;
}
void __fastcall __fastcall EICCurrencyFmt::Assign( const CURRENCYFMT &c)
{
    EICNumberFmt::Assign( *(NUMBERFMT*)&c);
    PositiveOrder   = c.PositiveOrder;
    CurrencySymbol  = c.lpCurrencySymbol;
}

//---------------------------------------------------------------------------

__fastcall EICStyle::~EICStyle()
{
    if( m_Font)          delete m_Font;
}
__fastcall EICStyle::EICStyle( TFlux *f):TObject()
{
    bool b;
    f->Lit( m_Nom);
    f->Lit( b); if( b) {m_Font  = new EICFont( f);}
    f->Lit( m_BrushColor);
    f->Lit( m_BrushStyle);
    if( f->FileVersion < FILE_VERSION( 2, 1))
    {
      f->Lit( b);
      f->Lit( b);
    }
    if( f->FileVersion >= FILE_VERSION( 2, 1))
    {
      f->Lit( m_TailleFontModifiable);
      f->Lit( m_StyleFondModifiable);
    }
    if( f->FileVersion >= FILE_VERSION( 2, 3))
      f->Lit( *((int*)&m_Presentation));
    if( f->FileVersion >= FILE_VERSION( 2, 4))
      f->Lit( m_NomPoliceMofifiable);
    if( f->FileVersion >= FILE_VERSION( 2, 5))
      f->Lit( m_LstColorPerso);
}
void __fastcall EICStyle::Enregistre( TFlux *f)const
{
    f->Ecrit( m_Nom);
    f->Ecrit( m_Font!=NULL);   if( m_Font)  m_Font->Enregistre(f);
    f->Ecrit( m_BrushColor);
    f->Ecrit( m_BrushStyle);
    f->Ecrit( m_TailleFontModifiable);
    f->Ecrit( m_StyleFondModifiable);
    f->Ecrit( m_Presentation);
    f->Ecrit( m_NomPoliceMofifiable);
    f->Ecrit( m_LstColorPerso);
}
//------------------------------------------------------------------------------
EICListStyle* ETATAPI NewEICListStyle(void)          { return new EICListStyle();}
void          ETATAPI DeleteEICListStyle(EICListStyle* Lst) { delete Lst;}

__fastcall EICListStyle::EICListStyle( TFlux *f):TStringList()
{
  if( f) Lit( f);
}
__fastcall EICListStyle::~EICListStyle()
{
  ViderListe();
}
void __fastcall EICListStyle::Lit( TFlux *f)
{
    ViderListe();
    int nb; f->Lit( nb);
    for( int i = 0; i < nb; i++)
    {
        EICStyle *s = new EICStyle( f);
        AddObject( s->Nom, s);
    }
}
void __fastcall EICListStyle::Enregistre( TFlux *f)const
{
  #pragma warn -8037
    f->Ecrit( Count);
    for( int i = 0; i < Count; i++)
    {
        EICStyle *s = (EICStyle *)Objects[i];
        s->Enregistre( f);
    }
  #pragma warn .8037
}
void __fastcall EICListStyle::Assign( const EICListStyle *pLstStyle)
{
  TFlux    *f     = NULL;
  EICStyle *Style = NULL;

  ViderListe();
  #pragma warn -8037
    for( int i = 0; i < pLstStyle->Count; i++)
    {
      f = new TFlux();
      ((EICStyle*)pLstStyle->Objects[i])->Enregistre(f);
      f->Position = 0;
      Style = new EICStyle( f);
      AddObject( Style->Nom, Style);
      delete f;
    }
  #pragma warn .8037
}
void __fastcall EICListStyle::ViderListe( void)
{
  EICStyle *Style = NULL;
  while(Count)
  {
    Style = (EICStyle*)Objects[0];
    if( Style) { delete  Style; Style = NULL;}
    Delete(0);
  }
}
//------------------------------------------------------------------------------
